import Vue from 'vue'
import Vuex from 'vuex'

import {
	Login,
	afterCheckLogin,
	clientLogin,
	qryUserDetails,
	deptToLogin
} from '@/api/public/login'

import {
	getAddressList
} from '../api/order/order.js'
import {
	getUserInfoByToken,
} from '@/api/index/index';
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		hasLogin: false,
		indexType: '0',
		userInfo: {},
		header: {},
		//部门数据
		depRange:[]
	},
	mutations: {
		login: (state, provider) => {
			state.hasLogin = true;
		},
		saveDepRange:(state,provider)=>{
			state.depRange = provider;
		},
		setUserInfo: (state, provider) => {
			state.userInfo = provider;
			uni.setStorage({ //缓存用户登陆状态
				key: 'userInfo',
				data: provider
			})
		},
		logout(state) {
			// if(state.hasLogin) {
				state.header = {};
				state.hasLogin = false;
			// }
			state.userInfo = {};
			
			uni.removeStorage({
				key: 'userInfo'
			})
		},
		setHeader: (state, provider) => {
			state.header = { ...state.header,
				...provider
			};
			uni.setStorageSync('header',state.header);
		},
		setIndexType: (state, provider) => {
			state.indexType = provider;
		}
	},
	actions: {
		userLogin({
			commit
		}, info) {
			return new Promise((resolve, reject) => {
				Login(info).then(res => {
					if (res.code === 0) {
						console.log(res.data)
						commit('login', res.data);
						commit('setHeader',{                                                                     
							"Authorization": "Bearer " + res.data.authToken
						});
						uni.setStorageSync('token', res.data.authToken)
					} else {
						resolve(res);
					}
					return res;
				}).then((res) => {
					if (res.code !== 0) {
						return;
					}
					getAddressList({
						page: 1,
						limit: 1
					}).then(addr => {
						if (addr.code === 0) {
							uni.setStorage({
								key: 'areaId',
								data: addr.data.records[0].areaId
							})
							uni.setStorage({
								key: 'addrId',
								data: addr.data.records[0].id
							})
						}
					})
					qryUserDetails({}).then(res1 => {
						commit("setHeader", {
							'currentDeptName': encodeURIComponent(res1.data.deptInfo[0].deptName),
							'currentUserName': encodeURIComponent(res1.data.userInfo.userName),
							'deptName': encodeURIComponent(res1.data.bizDeptInfo.deptName)
						});
						console.log('回馈的部门信息',res1.data.userInfo);
						console.log('回馈的部门信息',res1.data.userInfo);
						commit("setUserInfo", res1.data.userInfo);
						uni.setStorage([{
							key: 'deptinfo',
							data: res1.data.bizDeptInfo
						}])
						resolve(res1);
					}).then((res1) => {

					})
				}).catch(err => {
					reject(err)
				})
			})
		},
		selectedDeptLogin({commit}, info) {
			return new Promise((resolve, reject) => {
				afterCheckLogin(info).then(res => {
					if (res.code === 0) {
						commit('login', res.data);
						commit('setHeader', {
							"Authorization": "Bearer " + res.data.authToken
						});
						uni.setStorageSync('token', res.data.authToken)
					} else {
						resolve(res);
					}
					return res;
				}).then((res) => {
					if (res.code !== 0) {
						return;
					}
					getAddressList({
						page: 1,
						limit: 1
					}).then(addr => {
						if (addr.code === 0) {
							uni.setStorage({
								key: 'areaId',
								data: addr.data.records[0].areaId
							})
							uni.setStorage({
								key: 'addrId',
								data: addr.data.records[0].id
							})
						}
					})
					qryUserDetails().then(res1 => {
						commit("setHeader", {
							'currentDeptName': encodeURIComponent(res1.data.deptInfo[0].deptName),
							'currentUserName': encodeURIComponent(res1.data.userInfo.userName),
							'deptName': encodeURIComponent(res1.data.bizDeptInfo.deptName)
						});
						res1.data.userInfo.deptNameList = res1.data.deptInfo[0].deptName
						console.log('res1.data.userInfo',res1.data.userInfo);
						commit("setUserInfo", res1.data.userInfo);
						uni.setStorage([{
							key: 'deptinfo',
							data: res1.data.deptInfo
						}])
						resolve(res1);
					})
				}).catch(err => {
					reject(err)
				})
			})
		},

		clientLogin({
			commit
		}) {
			return new Promise((resolve, reject) => {
				clientLogin().then(res => {
					if (res.code === 0) {
						commit('setHeader', {
							"Authorization": "Bearer " + res.data.authToken
						});
					}
					resolve(res);
				})
			})
		},
		header({
			commit
		}, header) {
			commit('setHeader', header);
		},
		wxIndexLoad({ commit }, info) {
			return new Promise((resolve, reject) => {
				getUserInfoByToken(info).then(res => {
					if (res.code == 0) {
						commit('login', res.data);
						commit('setHeader', {
							"Authorization": "Bearer " + res.data.authToken
						});
						uni.setStorageSync('token', res.data.authToken)
					} else {
						resolve(res);
					}
					return res;
				}).then(res => {
					if (res.code !== 0) {
						return;
					}
					getAddressList({
						page: 1,
						limit: 1
					}).then(addr => {
						if (addr.code === 0) {
							uni.setStorage({
								key: 'areaId',
								data: addr.data.records[0].areaId
							})
							uni.setStorage({
								key: 'addrId',
								data: addr.data.records[0].id
							})
						}
					})
					qryUserDetails().then(res1 => {
						commit("setHeader", {
							'currentDeptName': encodeURIComponent(res1.data.deptInfo[0].deptName),
							'currentUserName': encodeURIComponent(res1.data.userInfo.userName),
							'deptName': encodeURIComponent(res1.data.bizDeptInfo.deptName)
						});
						commit("setUserInfo", res1.data.userInfo);
						uni.setStorage([{
							key: 'deptinfo',
							data: res1.data.deptInfo
						}])
						resolve(res1);
					})
				}).catch(err => reject(err))
			})
		},
		wxSelectDept({ commit }, info) {
			return new Promise((resolve, reject) => {
				deptToLogin(info).then(res => {
					if (res.code == 0) {
						commit('login', res.data);
						commit('setHeader', {
							"Authorization": "Bearer " + res.data.authToken
						});
						uni.setStorageSync('token', res.data.authToken)
					} else {
						resolve(res);
					}
					return res;
				}).then(res => {
					if (res.code !== 0) {
						return;
					}
					getAddressList({
						page: 1,
						limit: 1
					}).then(addr => {
						if (addr.code === 0) {
							uni.setStorage({
								key: 'areaId',
								data: addr.data.records[0].areaId
							})
							uni.setStorage({
								key: 'addrId',
								data: addr.data.records[0].id
							})
						}
					})
					qryUserDetails().then(res1 => {
						commit("setHeader", {
							'currentDeptName': encodeURIComponent(res1.data.deptInfo[0].deptName),
							'currentUserName': encodeURIComponent(res1.data.userInfo.userName),
							'deptName': encodeURIComponent(res1.data.bizDeptInfo.deptName)
						});
						commit("setUserInfo", res1.data.userInfo);
						uni.setStorage([{
							key: 'deptinfo',
							data: res1.data.deptInfo
						}])
						resolve(res1);
					})
				}).catch(err => reject(err))
			});
		},
	}
})

export default store
